# -*- coding:utf-8 -*-
from . import api
from flask import request, current_app, jsonify, session,g
from ihome import redis_store, db
from ihome.models import User
from ihome.utils.response_code import RET
from ihome.utils.storage_image import storage_image
from ihome.constants import QINIU_DOMIN_PREFIX
from ihome.utils.common import login_required

@api.route("/user/avatar",methods =["POST"])
@login_required
def upload_avatar():
    try:
        avatar_file = request.files.get("avatar").read()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR,errmsg="清选择图片")
    try:
        key = storage_image(avatar_file)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR,errmsg="上传文件失败")

    try:
        user = User.query.get(session["user_id"])
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="查询用户失败")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="用户名错误")

    user.avatar_url = key
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollBack()
        return jsonify(errno=RET.DBERR, errmsg="保存用户头像失败")

    return jsonify(errno=RET.OK,errmsg="ok",data = {"avatar_url":QINIU_DOMIN_PREFIX+key})

@api.route("/user")
@login_required
def get_user_info():
    # user_id = session.get("user_id")

    user_id = g.user_id
    print user_id
    try:

        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="查询数据库失败")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="没有该用户")

    return jsonify(errno=RET.OK, errmsg="ok",data= user.to_dict())

@api.route("/user/name",methods=["POST"])
@login_required
def set_user_name():
    data_dict = request.json
    name = data_dict.get("name")
    if not name:
        return jsonify(errno=RET.PARAMERR, errmsg="数据不完整")

    # user_id = session.get("user_id")
    user_id = g.user_id

    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据失败")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="用户不存在")

    try:
        user.name = name
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存用户失败")

    session["name"] = name
    return jsonify(errno=RET.OK, errmsg="修改成功")

@api.route("/user/auth")
@login_required
def get_user_auth():
    user_id = g.user_id
    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno = RET.DBERR,errmsg="查询用户失败")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="用户不存在")

    return jsonify(errno = RET.OK,errmsg = "ok",data = user.to_auth_dict())

@api.route("/user/auth",methods=["POST"])
@login_required
def set_user_auth():
    user_id = g.user_id
    data_dict = request.json
    real_name = data_dict.get("real_name")
    id_code = data_dict.get("id_card")

    print real_name,id_code
    if not all([real_name,id_code]):
        return jsonify(errno = RET.PARAMERR,errmsg = "参数错误")

    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno = RET.DBERR,errmsg="查询用户失败")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="用户不存在")

    user.real_name = real_name
    user.id_card = id_code
    try:
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error()
        return jsonify(errno = RET.DBERR,errmsg = "保存数据失败")

    return jsonify(errno = RET.OK,errmsg = "ok",data = user.to_auth_dict())

